// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "Rectangle.h"

Rectangle::Rectangle(double s, const QString &name) :
    Item(name)
{
    createRectangle(s,s,s);
}

Rectangle::Rectangle(double width, double height, double depth, const QString &name) :
    Item(name)
{
    createRectangle(width, height, depth);
}

void Rectangle::onFrameChanged(double frame)
{
    Q_UNUSED(frame);
    /* FIXME complete the code */
    // (optional)
    // apply a transformation matrix on this item using setGLTransform()
    // and frame to make the cube move.
    //Matrix m = Matrix::rotation(frame/10, frame/10, frame/10, frame/10);
    //setTransform(m);
    /* STOP here */
}

void Rectangle::createRectangle(double w, double h, double d)
{
    /* FIXME complete the code */
    // NOTE : suggestion for naming variables :
    //        TLF = Top Left Front
    //        BRB = Bottom Right Back
    //        etc...

    // 1) Define the 8 edge points of the cube as vectors
    Vector TLB(-w/2, h/2, -d/2); //Top Left Back
    Vector TLF(-w/2, h/2, d/2); //Top Left Front
    Vector TRB(w/2, h/2, -d/2); //Top Right Back
    Vector TRF(w/2, h/2, d/2); //Top Right Front

    Vector BLB(-w/2, -h/2, -d/2); //Bottom Left Back
    Vector BLF(-w/2, -h/2, d/2); //Bottom Left Front
    Vector BRB(w/2, -h/2, -d/2); //Bottom Right Back
    Vector BRF(w/2, -h/2, d/2); //Bottom Right Front

    // 2) Define the normal vector for each of the 6 faces
    Vector NBT(0.0, 1.0, 0.0); //Vector Bottom -> Top
    Vector NTB(0.0, -1.0, 0.0); //Vector Top -> Bottom

    Vector NLR(1.0, 0.0, 0.0); //Vector Left -> Right
    Vector NRL(-1.0, 0.0, 0.0); //Vector Right -> Left

    Vector NBF(0.0, 0.0, 1.0); //Vector Back -> Front
    Vector NFB(0.0, 0.0, -1.0); //Vector Front -> Back

    // 3) (optional) Define the texture vectors that will be applied on the cube
    //    This can be the same for each face (making the texture repeat on each face)
    //    or the texture can be 'wrapped' around the cube.
    /*
     *TTL===================TTR
     *                     =
     *       TEXTURE       =
     *                     =
     *TBL===================TBR
    */
    Vector TTL(0.0, 0.0, 0.0);
    Vector TTR(0.0, 1.0, 0.0);
    Vector TBR(1.0, 1.0, 0.0);
    Vector TBL(1.0, 0.0, 0.0);

    // 4) Define the 6 faces of the cube (Use the Face class)
    Face FT(Face::Quads); //Face Top
    Face FBT(Face::Quads); //Face BotTom
    Face FL(Face::Quads); //Face Left
    Face FR(Face::Quads); //Face Right
    Face FF(Face::Quads); //Face Front
    Face FB(Face::Quads); //Face Back

    //Use Face::addPoint(point, normal, texture) to define each point of the face.
    //Add 4 points pour la face dessous avec vector Top -> Bottom
    FBT.addPoint(BLB, NTB, TTL);
    FBT.addPoint(BLF, NTB, TTR);
    FBT.addPoint(BRF, NTB, TBR);
    FBT.addPoint(BRB, NTB, TBL);

    //Add point pour la face dessus avec vector Bottom -> Top
    FT.addPoint(TLB, NBT, TTL);
    FT.addPoint(TLF, NBT, TTR);
    FT.addPoint(TRF, NBT, TBR);
    FT.addPoint(TRB, NBT, TBL);

    //Add point la face LEFT avec vector Right -> Left
    FL.addPoint(TLB, NRL, TTL);
    FL.addPoint(TLF, NRL, TTR);
    FL.addPoint(BLF, NRL, TBR);
    FL.addPoint(BLB, NRL, TBL);

    //Add point pour la face RIGHT avec vector Left -> Right
    FR.addPoint(TRB, NLR, TTL);
    FR.addPoint(TRF, NLR, TTR);
    FR.addPoint(BRF, NLR, TBR);
    FR.addPoint(BRB, NLR, TBL);

    //Add point pour la face FONT avec vector Back -> Front
    FF.addPoint(TLF, NBF, TTL);
    FF.addPoint(TRF, NBF, TTR);
    FF.addPoint(BRF, NBF, TBR);
    FF.addPoint(BLF, NBF, TBL);

    //Add point pour la face BACK avec vector Front -> Back
    FB.addPoint(TLB, NFB, TTL);
    FB.addPoint(TRB, NFB, TTR);
    FB.addPoint(BRB, NFB, TBR);
    FB.addPoint(BLB, NFB, TBL);


    //You can use Face::setColor() to set the color for each face
    //if glColorMaterial is enabled.
    FT.setColor(QColor("red"));
    FBT.setColor(QColor("blue"));
    FL.setColor(QColor("green"));
    FR.setColor(QColor("yellow"));
    FB.setColor(Qt::magenta);
    FF.setColor(QColor("cyan"));

    // 5) Add the 6 faces to this item using addFace()
    this->addFace(FT);
    this->addFace(FBT);
    this->addFace(FL);
    this->addFace(FR);
    this->addFace(FB);
    this->addFace(FF);
    /* STOP here */
}

